package com.xunyun.kotlinwc.main.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.xunyun.kotlinwc.R
import com.xunyun.kotlinwc.base.BaseFragment
import com.xunyun.kotlinwc.databinding.FragmentProjectBinding
import com.xunyun.kotlinwc.main.adapter.ArticlePageAdapter
import com.xunyun.kotlinwc.main.adapter.ProjectPageAdapter
import com.xunyun.kotlinwc.main.model.Project
import com.xunyun.kotlinwc.main.viewmodel.ProjectViewModel
import com.xunyun.kotlinwc.utils.ToastUtils
import com.xunyun.kotlinwc.utils.paging.DiffCallback


class ProjectFragment : BaseFragment<FragmentProjectBinding, ProjectViewModel>() {

    private val adapter by lazy { ProjectPageAdapter(mContext,DiffCallback()) }
    override fun getLayoutId(): Int {
        return R.layout.fragment_project
    }

    override fun getViewModel(): ProjectViewModel {
        return ProjectViewModel()
    }

    override fun bindViewModel() {
        mViewModel.bindLifecycleOwner(this, 294)

    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        mBinding.recyclerView.layoutManager = LinearLayoutManager(mContext)
        mBinding.recyclerView.adapter = adapter;
        mBinding.refreshLayout.setOnRefreshListener {
            mViewModel.refresh()
        }

        mViewModel.mListLiveData.observe(this, Observer {
            adapter.submitList(it)
            if (mBinding.refreshLayout.isRefreshing) {
                mBinding.refreshLayout.isRefreshing = false;
            }

        })

        mViewModel.infoMsg.observe(this, Observer {
            ToastUtils.showInfo(it)
        })

    }


}